#include "mpHttpClientImpl.h"
#include "mpHttpDispatcher.h"
#include "mpLog.h"


namespace MP{
namespace HTTP{
namespace INTERNAL{

//HttpClientImpl::HttpClientImpl()
//    : HttpClient()
//{
//    start();
//}

//HttpClientImpl::HttpClientImpl()
//    : HttpClient(),
//      _httpWorkContext_ptr_u(new HttpWorkContext)
//{
//    _httpWorkContext = _httpWorkContext_ptr_u.get();

//    _httpConnectionManage = createHttpConnectionManage(_httpWorkContext->getIoContext());
//}

//HttpClientImpl::HttpClientImpl(HttpWorkContext& httpWorkContext)
//    : HttpClient(),_httpWorkContext(&httpWorkContext)
//{
//    _httpConnectionManage = createHttpConnectionManage(_httpWorkContext->getIoContext());
//}

HttpClientImpl::HttpClientImpl()
    : HttpClient(),_io_context()
{
    _httpConnectionManage = createHttpConnectionManage(_io_context);
}

HttpClientImpl::~HttpClientImpl(){
    //关闭所有请求，如果有正在运行的HTTP请求将报出HTTP连接异常
    _httpConnectionManage->clearAndDeactivate();
}

void HttpClientImpl::send(HttpRequestMessage::ptr_s httpRequest,
                          HttpResponseMessage::ptr_s httpResponse,
                          func_send_progress_callback&& progress_callback){

    std::shared_ptr<HttpDispatcherAsync> dispatcherAsync(std::make_shared<HttpDispatcherAsync>(_io_context,
                                                                                               _httpConnectionManage,
                                                                                               httpRequest,
                                                                                               httpResponse,
                                                                                               get_connect_timeout(),
                                                                                               get_write_timeout(),
                                                                                               get_read_timeout()));
    if(progress_callback){
        dispatcherAsync->set_send_progress_callback(std::move(progress_callback));
    }

    _io_context.restart();

    if(dispatcherAsync->execute_request_async()){
        _io_context.run();
    }



    //    try
    //    {

    //    }
    //    catch (HttpException& e)
    //    {
    //        httpRequest->getHttpRequestContent()->endWriteData();
    //        httpResponse->getHttpResponseContent()->endReadData();

    //        MPLOG_ERROR << "HttpClientImpl send err ,http exception: " << e.what();
    //    }
    //    catch (std::exception& e)
    //    {
    //        httpRequest->getHttpRequestContent()->endWriteData();
    //        httpResponse->getHttpResponseContent()->endReadData();

    //        MPLOG_ERROR << "HttpClientImpl send err ,standard exception: " << e.what();
    //    }
    //    catch (...)
    //    {
    //        httpRequest->getHttpRequestContent()->endWriteData();
    //        httpResponse->getHttpResponseContent()->endReadData();

    //        MPLOG_ERROR << "HttpClientImpl send err ,unkown error .";
    //    }

}

//head
HttpResponseMessage::ptr_s HttpClientImpl::head(const std::string& relativeUri,
                                                func_send_progress_callback&& progress_callback)
{
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_HEAD);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    if(progress_callback){
        send(httpRequest,httpResponse,std::move(progress_callback));
    }
    else{
        send(httpRequest,httpResponse);
    }
    //返回结果
    return httpResponse;
}

//get
HttpResponseMessage::ptr_s HttpClientImpl::get(const std::string& relativeUri,
                                               func_send_progress_callback&& progress_callback){

    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_GET);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    if(progress_callback){
        send(httpRequest,httpResponse,std::move(progress_callback));
    }
    else{
        send(httpRequest,httpResponse);
    }
    //返回结果
    return httpResponse;
}

bool HttpClientImpl::getStream(const std::string& relativeUri,
                               std::stringstream& stream,
                               func_send_progress_callback&& progress_callback) {

    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_GET);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseStreamMessage(stream);
    //发送请求
    if(progress_callback){
        send(httpRequest,httpResponse,std::move(progress_callback));
    }
    else{
        send(httpRequest,httpResponse);
    }
    //返回结果
    return httpResponse->isSuccess();
}

bool HttpClientImpl::download(const std::string& relativeUri,
                              const std::string& filePath,
                              std::size_t fileOffset,
                              func_send_progress_callback&& progress_callback) {
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_GET);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseFileMessage(filePath,fileOffset);
    //发送请求
    if(progress_callback){
        send(httpRequest,httpResponse,std::move(progress_callback));
    }
    else{
        send(httpRequest,httpResponse);
    }
    //返回结果
    return httpResponse->isSuccess();
}

//post
HttpResponseMessage::ptr_s HttpClientImpl::post(const std::string& relativeUri,
                                                HttpRequestContent::ptr_s content,
                                                func_send_progress_callback&& progress_callback) {
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_POST);
    initHttpRequestMessageHeaders(httpRequest);

    httpRequest->setHttpRequestContent(content);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    if(progress_callback){
        send(httpRequest,httpResponse,std::move(progress_callback));
    }
    else{
        send(httpRequest,httpResponse);
    }
    //返回结果
    return httpResponse;
}

//put
HttpResponseMessage::ptr_s HttpClientImpl::put(const std::string& relativeUri,
                                               HttpRequestContent::ptr_s content,
                                               func_send_progress_callback&& progress_callback) {
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_PUT);
    initHttpRequestMessageHeaders(httpRequest);

    httpRequest->setHttpRequestContent(content);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    if(progress_callback){
        send(httpRequest,httpResponse,std::move(progress_callback));
    }
    else{
        send(httpRequest,httpResponse);
    }
    //返回结果
    return httpResponse;
}

//delete
HttpResponseMessage::ptr_s HttpClientImpl::del(const std::string& relativeUri,
                                               func_send_progress_callback&& progress_callback) {
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_DELETE);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    if(progress_callback){
        send(httpRequest,httpResponse,std::move(progress_callback));
    }
    else{
        send(httpRequest,httpResponse);
    }
    //返回结果
    return httpResponse;
}




}   //INTERNAL
}   //HTTP
}   //MP
